package com.zuilapsa.jmc.exporters;


import bsh.Interpreter;

import com.zuilapsa.jmc.*;
import com.zuilapsa.jmc.model.*;
import com.zuilapsa.jmc.userInterfaces.UserInterfaceSession;

public class ExporterBean implements ExporterInterface {
	String name;
	String script;
	public ExporterBean(String name, String script) {
		this.name = name;
		try {
			this.script = Configuration.getInstance().getRootConfigElement().selectSingleNode("/level[@name='root']/level[@name='config']/level[@name='jmc']/property[@name='bshdir']/@value").getStringValue()+"/"+script;
		} catch (JmcException e) {
			System.err.println("Unable to read /root/config/jmc/bshdir :"+e.getMessage());
			this.script = script;
		}
	}
	public void export(UserInterfaceSession interfaceSession) throws JmcException {
		Interpreter i = new Interpreter();
		try {
			i.set("config", Level.getRootInstance().getConfigElement());
			//config does not have default values!!!
			//let's see if we can use model
			i.set("model", Level.getRootInstance());
			i.set("session", interfaceSession);
			i.set("script", script);
			i.set("name", name);
			
//			BshClassManager classManager = BshClassManager.createClassManager(i);
//			NameSpace global = new NameSpace(classManager,
//				"embedded BeanShell interpreter");
//			
//
//			NameSpace namespace = new NameSpace(global, "exporterNS");
//			namespace.setVariable("dupa2", "ass", false);
//			namespace.setVariable("session", interfaceSession,false);
//			Reader in = new FileReader(script);
//			i.eval(in, namespace, script);
			i.source(script);
		} catch (Exception e) {
			System.err.println("error executiong script "+script+": "+e);
		}	
	}
}
